package com.yehongyu.mytools.ui.awt;

//: ToeTest.java
//Demonstration of dialog boxes
//and creating your own components
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Dialog;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class ToeButton extends Canvas {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	int state = ToeDialog.BLANK;

	ToeDialog parent;

	ToeButton(ToeDialog parent) {
		this.parent = parent;
	}

	public void paint(Graphics g) {
		int x1 = 0;
		int y1 = 0;
		int x2 = getSize().width - 1;
		int y2 = getSize().height - 1;
		g.drawRect(x1, y1, x2, y2);
		x1 = x2 / 4;
		y1 = y2 / 4;
		int wide = x2 / 2;
		int high = y2 / 2;
		if (state == ToeDialog.XX) {
			g.drawLine(x1, y1, x1 + wide, y1 + high);
			g.drawLine(x1, y1 + high, x1 + wide, y1);
		}
		if (state == ToeDialog.OO) {
			g.drawOval(x1, y1, x1 + wide / 2, y1 + high / 2);
		}
	}

	public boolean mouseDown(Event evt, int x, int y) {
		if (state == ToeDialog.BLANK) {
			state = parent.turn;
			parent.turn = (parent.turn == ToeDialog.XX ? ToeDialog.OO
					: ToeDialog.XX);
		} else
			state = (state == ToeDialog.XX ? ToeDialog.OO : ToeDialog.XX);
		repaint();
		return true;
	}
}

class ToeDialog extends Dialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// w = number of cells wide
	// h = number of cells high
	static final int BLANK = 0;

	static final int XX = 1;

	static final int OO = 2;

	int turn = XX; // Start with x's turn

	public ToeDialog(Frame parent, int w, int h) {
		super(parent, "The game itself", false);
		setLayout(new GridLayout(w, h));
		for (int i = 0; i < w * h; i++)
			add(new ToeButton(this));
		setSize(w * 50, h * 50);
	}

}

public class ToeTest extends Frame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	TextField rows = new TextField("3");
	TextField cols = new TextField("3");
	Button b = new Button("go");

	public ToeTest() {
		setTitle("Toe Test");
		Panel p = new Panel();
		p.setLayout(new GridLayout(2, 2));
		p.add(new Label("Rows", Label.CENTER));
		p.add(rows);
		p.add(new Label("Columns", Label.CENTER));
		p.add(cols);
		add("North", p);
		b.addActionListener(this);
		add("South", b);
	}

//	public boolean action(Event evt, Object arg) {
//		if (arg.equals("go")) {
//			Dialog d = new ToeDialog(this, Integer.parseInt(rows.getText()),
//					Integer.parseInt(cols.getText()));
//			d.setVisible(true);
//		} else
//			return super.action(evt, arg);
//		return true;
//	}

	public static void main(String[] args) {
		Frame f = new ToeTest();
		f.setSize(200, 100);
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(b)) {
			final Dialog d = new ToeDialog(this, Integer.parseInt(rows.getText()),
					Integer.parseInt(cols.getText()));
			d.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					d.dispose();
				}
			});
			d.setVisible(true);
		} else{
			System.out.println("Object is [" + e.getSource()
					+ "],Unknown Action!");
		}
	}
}
